package com.friend.campusnotice;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.friend.campusnotice.bean.CallLog;
import com.friend.campusnotice.bean.Contacts;
import com.friend.campusnotice.util.MyUtil;

import java.util.ArrayList;
import java.util.List;

import cn.pedant.SweetAlert.SweetAlertDialog;

public class ContactDetailActivity extends AppCompatActivity implements View.OnClickListener {

    private Contacts contacts;
    private MyUtil myutil;


    TextView phoneNumber;
    TextView contactName;
    TextView editContact;
    TextView deleteContact;
    TextView callRecord;
    TextView phoneType;
    TextView numberLocation;
    ImageView sendMsg;
    Button saveEdit;
    Button cancelEdit;
    EditText editName;
    EditText editNumber;
    LinearLayout modefiyView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_detail);
        myutil = new MyUtil();
        // contacts =(Contacts) getIntent().getParcelableExtra("contact");
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            contacts = bundle.getParcelable("contact");
            Log.i("xiaohei", contacts.toString());
        }

        initView();

    }

    private void initView() {

        phoneNumber = (TextView) findViewById(R.id.phone_number);
        contactName = (TextView) findViewById(R.id.detail_name);
        editContact = (TextView) findViewById(R.id.edit_contact);
        phoneType = (TextView) findViewById(R.id.phone_type);
        numberLocation = (TextView) findViewById(R.id.phone_location);
        deleteContact = (TextView) findViewById(R.id.delete_record);
        callRecord = (TextView) findViewById(R.id.contact_call_record);
        sendMsg = (ImageView) findViewById(R.id.send_msg);
        saveEdit = (Button) findViewById(R.id.save_edit);
        cancelEdit = (Button) findViewById(R.id.cancel_edit);
        editName = (EditText) findViewById(R.id.edit_contact_name);
        editNumber = (EditText) findViewById(R.id.edit_contact_input);
        modefiyView = (LinearLayout) findViewById(R.id.modify_view);

        RelativeLayout contactInfo = (RelativeLayout) findViewById(R.id.contact_info);
        if (contacts != null) {
            phoneNumber.setText(contacts.getPhoneNumber());
            contactName.setText(contacts.getContactName());
            Toast.makeText(ContactDetailActivity.this, contacts.getContact_Id() + "", Toast.LENGTH_SHORT).show();
        }
        contactInfo.setOnClickListener(this);
        editContact.setOnClickListener(this);
        deleteContact.setOnClickListener(this);
        callRecord.setOnClickListener(this);
        sendMsg.setOnClickListener(this);
        saveEdit.setOnClickListener(this);
        cancelEdit.setOnClickListener(this);


    }


    private void showDefaulMode() {
        phoneNumber.setVisibility(View.VISIBLE);
        contactName.setVisibility(View.VISIBLE);
        editContact.setVisibility(View.VISIBLE);
        deleteContact.setVisibility(View.VISIBLE);
        callRecord.setVisibility(View.VISIBLE);
        phoneType.setVisibility(View.VISIBLE);
        numberLocation.setVisibility(View.VISIBLE);
        sendMsg.setVisibility(View.VISIBLE);
//        saveEdit.setVisibility(View.GONE);
//        cancelEdit.setVisibility(View.GONE);
        modefiyView.setVisibility(View.GONE);
        editName.setVisibility(View.GONE);
        editNumber.setVisibility(View.GONE);
    }

    private void showEditMode() {
        phoneNumber.setVisibility(View.GONE);
        contactName.setVisibility(View.GONE);
        editContact.setVisibility(View.GONE);
        deleteContact.setVisibility(View.GONE);
        callRecord.setVisibility(View.GONE);
        phoneType.setVisibility(View.GONE);
        numberLocation.setVisibility(View.GONE);
        sendMsg.setVisibility(View.GONE);
//        saveEdit.setVisibility(View.VISIBLE);
//        cancelEdit.setVisibility(View.VISIBLE);
        modefiyView.setVisibility(View.VISIBLE);
        editName.setVisibility(View.VISIBLE);
        editName.setText(contacts.getContactName());
        editNumber.setVisibility(View.VISIBLE);
        editNumber.setText(contacts.getPhoneNumber());

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.contact_info:  // 拨打电话
                callContact();
                break;
            case R.id.edit_contact: // 编辑联系人
                showEditMode();
                break;
            case R.id.delete_record: // 删除联系人
                deleteContacts();
                break;
            case R.id.contact_call_record: // 通话记录
                // retrieveThisGuyCallLog();

                Intent intent = new Intent(ContactDetailActivity.this, CallLogActivity.class);
                Bundle bundle = new Bundle();
                bundle.putParcelable("contact", contacts);
                intent.putExtras(bundle);
                startActivity(intent);

                break;
            case R.id.send_msg: // 发短信
                Toast.makeText(ContactDetailActivity.this, "发短信", Toast.LENGTH_SHORT).show();
                Intent intentmsg = new Intent(ContactDetailActivity.this, SendMSGActivity.class);
                startActivity(intentmsg);
                break;
            case R.id.save_edit: // 保存

                break;
            case R.id.cancel_edit: // 取消
                showDefaulMode();
                break;
        }
    }

//    private void retrieveThisGuyCallLog() {
//        List<CallLog> callLogs = new ArrayList<CallLog>();
//        callLogs =  myutil.getSingleCall(ContactDetailActivity.this,contacts.getPhoneNumber());
//
//
//    }

    private void deleteContacts() {

        new SweetAlertDialog(ContactDetailActivity.this, SweetAlertDialog.WARNING_TYPE)
                .setTitleText("确定删除该联系人吗?")
                .setConfirmText("确定")
                .setCancelText("取消")
                .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sDialog) {
                        sDialog.dismissWithAnimation();
                        Boolean isdelete = myutil.deleteContact(contacts.getPhoneNumber(), ContactDetailActivity.this);
                        if (isdelete) {
                            Intent intent = new Intent();
                            intent.putExtra("name", "yes");// 放入返回值
                            setResult(0, intent);// 放入回传的值,并添加一个Code,方便区分返回的数据
                            finish();
                        }
                    }
                })
                .setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                        sweetAlertDialog.dismissWithAnimation();
                    }
                })
                .show();

    }

    private void callContact() {

        //用intent启动拨打电话
        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + contacts.getPhoneNumber()));

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        startActivity(intent);

    }
}
